using System;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Input;
using Application = System.Windows.Application;

namespace DesktopWidgetApp.UI.Helpers
{
    /// <summary>
    /// 系统托盘图标管理器
    /// 负责创建和管理应用程序的系统托盘图标
    /// </summary>
    public class TrayIconManager : IDisposable
    {
        private NotifyIcon _trayIcon;
        private Window _mainWindow;
        private bool _isDisposed = false;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainWindow">主窗口</param>
        public TrayIconManager(Window mainWindow)
        {
            _mainWindow = mainWindow;
            InitializeTrayIcon();
        }

        /// <summary>
        /// 初始化系统托盘图标
        /// </summary>
        private void InitializeTrayIcon()
        {
            _trayIcon = new NotifyIcon
            {
                Text = "工位状态挂件",
                Icon = CreateDefaultIcon(),
                Visible = true
            };

            // 创建上下文菜单
            var contextMenu = new ContextMenuStrip();
            
            // 显示窗口菜单项
            var showMenuItem = new ToolStripMenuItem("显示");
            showMenuItem.Click += (sender, e) =>
            {
                _mainWindow.Dispatcher.Invoke(() =>
                {
                    _mainWindow.Show();
                    _mainWindow.WindowState = WindowState.Normal;
                    _mainWindow.Activate();
                });
            };
            contextMenu.Items.Add(showMenuItem);

            // 退出菜单项
            var exitMenuItem = new ToolStripMenuItem("退出");
            exitMenuItem.Click += (sender, e) =>
            {
                _mainWindow.Dispatcher.Invoke(() =>
                {
                    Application.Current.Shutdown();
                });
            };
            contextMenu.Items.Add(exitMenuItem);

            _trayIcon.ContextMenuStrip = contextMenu;

            // 双击托盘图标显示窗口
            _trayIcon.DoubleClick += (sender, e) =>
            {
                _mainWindow.Dispatcher.Invoke(() =>
                {
                    if (_mainWindow.IsVisible)
                    {
                        _mainWindow.Hide();
                    }
                    else
                    {
                        _mainWindow.Show();
                        _mainWindow.WindowState = WindowState.Normal;
                        _mainWindow.Activate();
                    }
                });
            };
        }

        /// <summary>
        /// 创建默认图标
        /// </summary>
        /// <returns>图标对象</returns>
        private Icon CreateDefaultIcon()
        {
            // 创建一个简单的图标
            using (var bitmap = new Bitmap(16, 16))
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.Transparent);
                graphics.FillEllipse(Brushes.Green, 4, 4, 8, 8);
                return Icon.FromHandle(bitmap.GetHicon());
            }
        }

        /// <summary>
        /// 更新托盘图标状态
        /// </summary>
        /// <param name="isDoNotDisturb">是否免打扰</param>
        public void UpdateTrayIconStatus(bool isDoNotDisturb)
        {
            if (_trayIcon == null || _isDisposed)
                return;

            using (var bitmap = new Bitmap(16, 16))
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.Transparent);
                // 根据免打扰状态设置不同颜色的图标
                graphics.FillEllipse(isDoNotDisturb ? Brushes.Red : Brushes.Green, 4, 4, 8, 8);
                
                // 保存旧图标
                var oldIcon = _trayIcon.Icon;
                
                // 设置新图标
                _trayIcon.Icon = Icon.FromHandle(bitmap.GetHicon());
                
                // 释放旧图标资源
                if (oldIcon != null)
                    oldIcon.Dispose();
            }
        }

        /// <summary>
        /// 显示托盘通知
        /// </summary>
        /// <param name="title">通知标题</param>
        /// <param name="message">通知消息</param>
        /// <param name="notificationType">通知类型</param>
        public void ShowNotification(string title, string message, ToolTipIcon notificationType = ToolTipIcon.Info)
        {
            if (_trayIcon == null || _isDisposed)
                return;

            _trayIcon.ShowBalloonTip(3000, title, message, notificationType);
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            if (!_isDisposed)
            {
                if (_trayIcon != null)
                {
                    _trayIcon.Visible = false;
                    _trayIcon.Dispose();
                    _trayIcon = null;
                }
                _isDisposed = true;
            }
        }
    }
}